/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package toolz;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import DATA.iDataSet;
import DATA.iDataSetParts.Peak;
import DATA.iDataSetParts.Peaklist;
import DATA.iDataSetParts.SubstanceInfo;
import GUI.iFrames.DataSourceComponents.DataSourcesInternalFrame;

/**
 *
 * @author Bunkowski
 */
public class PolarGlyphBuilder {

    public static Image getPolarGlyphImage(Vector<String[]> values, int size) {
        final XYSeriesCollection dataset = new XYSeriesCollection();

        for (int i = 0; i < values.size(); i++) {
            String[] value = values.elementAt(i);
            XYSeries series;
            try {
                //if series for this class exists use it
                series = dataset.getSeries(value[0]);
                
            } catch (Exception ex) {
                //else create a new one for this class
                series = new XYSeries(value[0]);
                dataset.addSeries(series);
                //and set its first value to zero
                series.add(360d / values.size() * i, 0);
            }
            
            series.add(360d / values.size() * i, Double.parseDouble(value[1]));
            series.add(360d / values.size() * i, 0);
        }

        final JFreeChart chart = ChartFactory.createPolarChart(
                "", // chart title
                dataset, // data
                false, // include legend
                false,
                false);
        chart.setBackgroundPaint(new Color(255, 255, 255, 0));
        chart.getPlot().setBackgroundAlpha(0f);
        chart.setBorderVisible(false);

        PolarPlot polarPlot = (PolarPlot) chart.getPlot();
        polarPlot.setRadiusGridlinesVisible(false);
        polarPlot.setAngleGridlinesVisible(false);
        //polarPlot.setRadiusGridlinePaint(Color.black);
        polarPlot.getAxis().setVisible(false);
        polarPlot.setAngleLabelsVisible(false);
        polarPlot.setOutlineVisible(false);

        BufferedImage image = chart.createBufferedImage(size, size);


        return image;
    }

    public static Image getLivePolarPlot(DataSourcesInternalFrame dsif, SubstanceInfo liveSubstance) {
        Vector<String> ids = dsif.getMeasurementIds();
        int classIndex = dsif.selectedClasses;
        iDataSet idataset = dsif.idataset;
        Vector<String[]> glyphData = new Vector<String[]>(ids.size());



        for (int j = 0; j < ids.size(); j++) {
            Double d = 0.0;
            String id = ids.get(j);
            if (idataset.peaklists.containsKey(id)) {
                Peaklist pl = idataset.peaklists.get(id);
                Peak peak = liveSubstance.getBestHit(pl.getPeaks(), "dt");
                if (peak != null) {
                    d = peak.intensity;
                }
            }

            String klass = "";
            if (classIndex != -1) {
                klass = dsif.table.getValueAt(j, classIndex) + "";
            }

            glyphData.add(new String[]{klass+"",d+""});
        }
        return getPolarGlyphImage(glyphData, 200);
    }

}
